﻿using MCPlatform.Dxf.Entities;
using MCPlatform.Dxf.Interfaces;
using netDxf.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCPlatform.Dxf.Factories
{
    public class PathEntityFactoryManager
    {
        private readonly Dictionary<Type, IPathEntityFactory> _factories = new Dictionary<Type, IPathEntityFactory>();

        public PathEntityFactoryManager()
        {
            // Register the factories here
            RegisterFactory(new LinePathEntityFactory());
            RegisterFactory(new ArcPathEntityFactory());
            RegisterFactory(new CirclePathEntityFactory());
            RegisterFactory(new Polyline2DPathEntityFactory());
            RegisterFactory(new EllipsePathEntityFactory());
            RegisterFactory(new SplinePathEntityFactory());
        }


        private void RegisterFactory(IPathEntityFactory factory)
        {
            Type handledType = factory.GetHandledType();
            if (handledType != null)
            {
                _factories[handledType] = factory;
            }
            else
            {
                // 如果工厂没有提供处理的类型，则抛出异常或记录错误  
                throw new InvalidOperationException("The factory did not provide a handled type.");
            }
        }

        public PathEntityBase? CreatePathEntity(EntityObject entity)

        {
            if (entity != null && _factories.TryGetValue(entity.GetType(), out var factory))
            {
                return factory.CreatePathEntity(entity);
            }
            return null; // 或者抛出异常  
        }

        //public PathEntityBase CreatePathEntityWithoutParams(Type entityType)
        //{
        //    if (_factories.TryGetValue(entityType, out var factory))
        //    {
        //        return factory.CreatePathEntityWithoutParams();
        //    }
        //    return null; // 或者抛出异常  
        //}
    }
}
